// pages/search/search.js
var {
    getHomeGoods
} = require("../../api/Home")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        history: [], //搜索历史
        searchResult: [], //搜索结果
        antistop: '', //搜索的关键词
        fla: true
    },
    // 搜索的关键词绑定
    searchAntistop(e) {
        this.setData({
            antistop: e.detail.value
        })
        if (this.data.antistop == "") {
            this.setData({
                fla: true,
            })
        }
    },
    //搜索
    search() {
        let antistop = this.data.antistop;
        if (this.data.antistop == "") {
            wx.showToast({
                title: "请输入关键词",
                icon: "error",
                duration: 2000
            })
            this.setData({
                fla: true,
            })
            return;
        } else {
            getHomeGoods({
                query: antistop
            }).then(data => {
                let lists = [];
                let goodslists = data.message.goods;
                goodslists.forEach(item => {
                    let {
                        goods_id,
                        goods_small_logo,
                        goods_name,
                        goods_price
                    } = item;
                    let data = {
                        id: goods_id,
                        goodsName: goods_name,
                        goodsPrice: goods_price,
                        goodsImage: goods_small_logo,
                        originalPrice: goods_price + 100,
                    };
                    if (goods_small_logo) {
                        lists.push(data);
                    }
                })
                this.setData({
                    searchResult: lists
                })
            })
            let historys = this.data.history;
            if(!historys){
               this.setData({
                fla: false,
                history:[antistop],
            }) 
            }else{
               historys.unshift(antistop);
            this.setData({
                fla: false,
                history:historys,
            })  
            }
        }
    },
    // 添加到购物车
    addShopping(options) {
        let id = options.target.dataset.id;
        let index = this.data.searchResult.findIndex(item => item.id == id);
        // console.log(index);
        let cartLists = {
            id: this.data.searchResult[index].id,
            goodsImage: this.data.searchResult[index].goodsImage,
            goodsName: this.data.searchResult[index].goodsName,
            goodsPrice: this.data.searchResult[index].goodsPrice,
            num: 1,
        };
        let goodsCarList = wx.getStorageSync('goodsCarList');
        if (goodsCarList) {
            let i = goodsCarList.findIndex(item => item.id == id);
            if (i != -1) {
                goodsCarList[i].num += 1;
            } else {
                goodsCarList.push(cartLists);
            }
            wx.setStorageSync('goodsCarList', goodsCarList);
        } else {
            wx.setStorageSync('goodsCarList', [cartLists])
        }
        wx.showToast({
            title: "已添加到购物车",
            duration: 2000
        })
    },
    // 点击搜索历史
    searchHistory(e){
let index=e.currentTarget.dataset.index;
let value=this.data.history[index];
this.setData({
    antistop:value
})
    },
    // 删除搜索历史
    del(){
        this.setData({
            history:[] 
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let history = wx.getStorageSync('history');
        if(history){
this.setData({
            history,
        })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let history = wx.getStorageSync('history');
        if(history){
this.setData({
            history,
        })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.setStorageSync('history', this.data.history);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})